/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package productsrecord;

import dao.CategoryDAO;
import dao.ProductDAO;
import java.util.List;
import javax.persistence.EntityManager;
import model.Category;
import model.Product;

/**
 *
 * @author hendro
 */
public class Service {
    private EntityManager em;    
    private ProductDAO pDao;
    private CategoryDAO cDao;    
    public Service(EntityManager em){
       setEm(em);
    }
    private void setEm(EntityManager em) {
        this.em = em;
        this.pDao = new ProductDAO(this.em);
        this.cDao = new CategoryDAO(this.em);
    }    
    public void refresh(Object ebj){
        this.em.getTransaction().begin();
        this.em.refresh(ebj);
        this.em.getTransaction().commit();
    }    
    public void save(Product product){
        this.em.getTransaction().begin();
        pDao.save(product);
        this.em.getTransaction().commit();
    }    
    public void save(Category cat){
        this.em.getTransaction().begin();
        cDao.save(cat);
        this.em.getTransaction().commit();
    }    
    public void delete(Product product){
        this.em.getTransaction().begin();
        pDao.delete(product);
        this.em.getTransaction().commit();
    }    
    public void delete(Category cat){
        this.em.getTransaction().begin();
        cDao.delete(cat);
        this.em.getTransaction().commit();
    }    
    public Product getProduct(long id){
        return pDao.getProduct(id);
    }    
    public Category getCategory(long id){
        return cDao.getCategory(id);
    }    
    public List<Product> getProducts(){
        return pDao.getProducts();
    }    
    public List<Category> getCategories(){
        return cDao.getCategories();
    }
}
